package ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;

import ui.handlers.OrderHandler;
import ui.handlers.PayHandler;
import ui.handlers.ViewTransactionHandler;


import java.rmi.RemoteException;
import java.util.ArrayList;

public class ViewTransactionScreen extends BaseScreen{

	private JFrame mainFrame;
	
	private JPanel mainContent_panel;
	
	private JButton pending_btn, complete_btn;
	
	/* Methods needed by Handler */
	
	public JButton getPendingButton()
	{ return pending_btn; }
	
	public JButton getCompleteButton()
	{ return complete_btn; }
	
	/* END OF HANDLER METHODS */
	
	public ViewTransactionScreen()
	{
		mainFrame = new JFrame();
		mainFrame.setTitle("Cashier: View Transactions");
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setSize(650, 550);
		mainFrame.setLayout(new BorderLayout());
		
		mainContent_panel = new JPanel();
		
		pending_btn = new JButton("Pending");
		pending_btn.addActionListener(new OrderHandler(this));
		
		complete_btn = new JButton("Complete");
		complete_btn.addActionListener(new OrderHandler(this));
		
		doTitle();
		doButtons();
		
		mainFrame.setVisible(true);
	}
	
	private void doTitle()
	{
		String text = "<HTML><FONT SIZE = \"6\">"+
				"Transactions</FONT></HTML>";
		JLabel title_lbl = new JLabel(text);
		title_lbl.setBorder(new EmptyBorder(0, 0, 20, 0));
		mainFrame.add(title_lbl, BorderLayout.NORTH);
	}
	
	public void doMain(ArrayList<Integer> transactionsID)
	{
		mainFrame.remove(mainContent_panel);
		mainFrame.validate();
		
		mainContent_panel = new JPanel();
		mainContent_panel.setLayout(new FlowLayout());
		
		if (transactionsID.isEmpty())
		{ mainContent_panel.add(new JLabel("NO TRANSACTIONS")); }
		else
		{
			for (int transactionID : transactionsID)
			{
				JLabel name_lbl = null;
				try {
					name_lbl = new JLabel(store.getCustomerNameByTransaction(transactionID));
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				JPanel name_panel = new JPanel();
				name_panel.add(name_lbl);
				
				JButton view_btn = new JButton("Button");
				try {
					if (store.isTransactionComplete(transactionID))
					{
						view_btn.setText("View");
						view_btn.addActionListener(new ViewTransactionHandler(transactionID, this));
					}
					else if (store.isTransactionPending(transactionID))
					{
						view_btn.setText("Pay");
						view_btn.addActionListener(new PayHandler(transactionID, this));
					}
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				JPanel button_panel = new JPanel();
				button_panel.add(view_btn);
				
				JPanel trans_panel = new JPanel();
				trans_panel.setLayout(new GridLayout(1, 2));
				trans_panel.add(name_panel);
				trans_panel.add(button_panel);
				
				mainContent_panel.add(trans_panel);
			}
		}
		
		mainFrame.add(mainContent_panel, BorderLayout.CENTER);
		mainFrame.validate();
	}
	
	private void doButtons()
	{
		JPanel button_panel = new JPanel();
		button_panel.setLayout(new FlowLayout());
		
		button_panel.add(pending_btn);
		button_panel.add(complete_btn);
		
		mainFrame.add(button_panel, BorderLayout.SOUTH);
	}
}